package com.ht.controller;

import com.ht.data.Result;
import com.ht.domain.bo.ArticleBO;
import com.ht.domain.vo.ArticleVO;
import com.ht.em.ResultEnum;
import com.ht.service.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 科普文章
 */
@RestController
@RequestMapping("article")
@Api
@RefreshScope //开启自动刷新配置
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 根据科普文章id修改科普文章状态
     * @param articleId 文章id
     * @param articleStatus 状态
     * @return
     */
    @PutMapping("/{articleId}")
    @ApiOperation("根据科普文章id修改科普文章状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "articleId",value = "科普文章id",required = true,paramType = "path",dataTypeClass = String.class),
            @ApiImplicitParam(name = "articleStatus",value = "科普文章审核状态1通过,2:不通过",required = true,paramType = "query",dataTypeClass = String.class)
    })
    public Result updateArticleStatusByArticleId(@PathVariable("articleId")String articleId,@RequestParam("articleStatus") String articleStatus){
        return articleService.updateArticleStatusByArticleId(articleId,articleStatus);
    }

    /**
     * 查询所有科普文章
     * @return
     */
    @PostMapping("findArticleAll")
    @ApiOperation("查询所有科普文章")
    public Result findArticleAll(){
        List<ArticleBO> articleBOList = articleService.findArticleAll();
        //将bo类转成vo类返回数据到前台
        List<ArticleVO> articleVOList = new ArrayList<>();
        articleBOList.forEach((articleBO) -> {
            ArticleVO articleVO = new ArticleVO();
            BeanUtils.copyProperties(articleBO,articleVO);
            articleVOList.add(articleVO);
        });
        return Result.OK().setData(articleVOList);
    }

    /**
     * 根据科普文章类型查询
     * @param typeId 类型
     * @return
     */
    @ApiOperation("根据科普文章类型查询")
    @ApiImplicitParam(name = "typeId",value = "文章类型id",required = true,paramType = "path",dataTypeClass = String.class)
    @PostMapping("findArticleByTypeId/{typeId}")
    public Result findArticleByTypeId(@PathVariable("typeId")String typeId){
        List<ArticleBO> articleBOList = articleService.findArticleByTypeId(typeId);
        //判断是否查询到数据,如果没有数据返回状态码(未找到符合条件的数据)
        if (articleBOList.size() <= 0){
            return Result.response(ResultEnum.QUERY_NULL);
        }
        //将bo类转成vo类返回数据到前台
        List<ArticleVO> articleVOList = new ArrayList<>();
        articleBOList.forEach((articleBO) -> {
            ArticleVO articleVO = new ArticleVO();
            BeanUtils.copyProperties(articleBO,articleVO);
            articleVOList.add(articleVO);
        });
        return Result.OK().setData(articleVOList);
    }






}
